<?php
/**
 * Created by PhpStorm.
 * User: 72925
 * Date: 2018/11/27
 * Time: 15:40
 * order: 任务相关
 */

namespace app\index\controller;

use think\Controller;
use app\index\model\Contact;
use app\index\model\Advert;
use app\index\model\User;
use app\index\model\Fbtask;
use app\index\model\Task;


class Tasks extends Base{

	//任务大厅（普通任务和高级任务）
	public function commonlytask()
	{
		$userID = input('id');
		$sign = input('sign'); 
		$status = $this->status;
		if($status == "error"){
			$dat = ['code' => 403, 'msg' => '登录超时,请重新登录'];
	        return json_encode($dat);
		}else{
			//当前用户接过的单子
			$used = Task::where('uid',$userID)->where('status',1)->field(['fbid'])->select();
			$used = $used->toArray();
			// dump($used);
			$result = array_reduce($used, function($result,$value){
				return array_merge($result,array_values(($value)));
			},array());
			//创建查询语句
			$where['id'] =array('not in',$result);
			$sj = new Fbtask();		
			if($sign == "0" || $sign == "1"){
				
				$all = $sj->where('active','0')->where($where)->where('type',$sign)->where('num','neq','0')->field(['number','money','num','id'])->order('id','asc')->SELECT();
				$data =  $all->toArray();
				
					$dat = ['code' => 200, 'msg' => "返回数据成功",'task'=>$data];
                	return json_encode($dat);				
			}else{
				$dat = ['code' => 405, 'msg' => '参数错误，请传入任务标识'];
	            return json_encode($dat);
			}
		}						
	}




	/*任务详情*/
	public function taskdetail()
	{

		$taskid = input('number');	
		$istrue = Fbtask::where(['number'=>$taskid])->value('id');
		if($istrue !== null){
			$pic = Fbtask::where(['number'=>$taskid])->value('pic');
            $datpic = array('pic'=>explode("-",$pic));
            $data = Fbtask::get(['number'=>$taskid]);
            $res = $data->hidden(['number','id','type','pic','active'])->toArray();
            $newdata = array_merge($res,$datpic);            
            $dat = ['code' => 200, 'msg' => '返回数据成功',  $newdata];
            return json_encode($dat,TRUE);
		}else{
			$dat = ['code' => 403, 'msg' => '任务编号无效'];
            return json_encode($dat,TRUE);
		}
	}



	/*领取任务*/
	public function dotask()
	{
		//判断当前会员已经接个任务总金额
		//高级会员 40  普通会员 8
		//判断当前会员等级， 判断每天已结任务(进行中的和已完成的)
			$userID = input('id'); 
			$fbid = input('fbid');//任务id
			$status = $this->status;
			if($status == "error")
			{
				$dat = ['code' => 403, 'msg' => '登录超时,请重新登录'];
		        return json_encode($dat);
			}
			else
			{	
				//判断用户身份
				$user = new User;
				$fbtask = new Fbtask;
				$task = new Task;
				$taskid = $fbtask->where('id',$fbid)->where('active','0')->value('id'); //任务id	
				$sign = $fbtask->where('id',$fbid)->value('type'); //任务等级
				$ISpay = User::where('id',$userID)->value('grade'); //会员等级
				$money = $fbtask->where('id',$fbid)->value('money');//当前任务的金额
				$num =  $fbtask->where('id',$fbid)->value('num'); //任务数量
				$vip = 8;
				$svip = 40;
				$commission = db('task')->alias('ta')->join('user us','ta.uid = us.id')->join('fbtask fb','ta.fbid = fb.id')
							->where('ta.uid',$userID)
							->where('us.grade',$ISpay)
							->where('fb.active',0)
							->whereTime('create_time', 'today')
							->sum('money');//
				//判断当前任务的状态是否正在进行中
				$dat = $task->where('uid',$userID)->where('fbid',$fbid)->where('status',1)->select();					
				$res = $dat->hidden(['create_time','update_time'])->toArray();				
				if($ISpay !== "0"  && $taskid !== NULL ) //会员等级不为3 并且任务id不为空
				{
					if($ISpay == "1" &&  $sign == 1)
					{//普通会员高级任务				
						$dat = ['code' => 401, 'msg' => '您是普通会员，充值高级会员可接高级任务'];
		            	return json_encode($dat);
					}
					elseif($ISpay == "1" && $sign == 0 && $num !== 0)
					{	
						if(($vip - $commission) > 0 && ($vip - $commission) >= $money){								
							if($res == NULL){
								$Time = date('Y-m-d H:i:s');
								$task->data(['uid'=>$userID,'fbid'=>$fbid,'create_time'=> $Time]);
								 $insert = $task->save();	
								 $num = $sign = $fbtask->where('id',$fbid)->value('num'); //任务等级
								 $j = db('fbtask')->where('id',$fbid)->where('active',0)->setDec('num',1);							
								 if($insert == 1 && $j == 1){
 									//任务数量-1								 									 	
								 	$dat = ['code' => 200, 'msg' => '获取任务成功'];
            						return json_encode($dat,TRUE);
								 }

							}else{							
									$dat = ['code' => 402, 'msg' => '此任务正在进行中,完成后可再接'];
            						return json_encode($dat,TRUE);
							}
						}
						elseif( ($vip - $commission) == "0")
						{	
							$dat = ['code' => 405, 'msg' => '您今天没有可以做的任务啦'];
            				return json_encode($dat,TRUE);							
						}
						elseif(($vip - $commission) < $money )
						{
							$dat = ['code' => 406, 'msg' => '当前任务佣金超出范围,您今天还可以接佣金为".$data."元的任务'];
            				return json_encode($dat,TRUE);
						}		
						
					}
					elseif($ISpay == "2" && $num !== 0)//超级会员
					{
						if(($svip - $commission) > 0 && ($svip - $commission) >= $money){
							if($res == NULL){
								$Time = date('Y-m-d H:i:s');
								$task->data(['uid'=>$userID,'fbid'=>$fbid,'create_time'=> $Time]);
								 $insert = $task->save();	
								 $j = db('fbtask')->where('id',$fbid)->where('active',0)->setDec('num',1);							
								 if($insert == 1 && $j == 1){
 									//任务数量-1								 									 	
								 	$dat = ['code' => 200, 'msg' => '获取任务成功'];
            						return json_encode($dat,TRUE);
								 }

							}else{							
									$dat = ['code' => 402, 'msg' => '此任务正在进行中,完成后可再接'];
            						return json_encode($dat,TRUE);
							}
						}
						elseif( ($svip - $commission) == 0)
						{	
							$dat = ['code' => 405, 'msg' => '您今天可接任务已经达到上限'];
            				return json_encode($dat,TRUE);
							
						}
						elseif(($svip - $commission) < $money )
						{
							$dat = ['code' => 406, 'msg' => '当前任务佣金超出范围,您今天还可以接总佣金为".$data."元的任务'];
            				return json_encode($dat,TRUE);
						}		
					}
					else
					{
						$dat = ['code' => 408, 'msg' => '当前任务剩余数量为0'];
            			return json_encode($dat,TRUE);
					}	
				}
				elseif($ISpay == "0" )
				{
					
					$dat = ['code' => 407, 'msg' => '您还不是会员，请充值'];
            		return json_encode($dat,TRUE);
				}
				elseif( $taskid == NULL)
				{
					
					$dat = ['code' => 404, 'msg' => '此任务不存在或者已经下线'];
            		return json_encode($dat,TRUE);
				}
				
			}

	}

	

	//取消任务
	public function cancle()
	{
		$userID = input('id');
		$taskid = input('taskid');
		$status = $this->status;
		$stat = 3;
		if($status == "error"){
			$dat = ['code' => 403, 'msg' => '登录超时,请重新登录'];
	        return json_encode($dat);
		}else{
			$res = db('task')->where('id',$taskid)->value('status');
			if($res !== "1"){
				$status = Task::where('id',$taskid)->update(['status'=>$stat,'reason'=>'用户取消任务']);
				if($status){
					$dat = ['code' => 200, 'msg' => '任务取消成功'];
	        		return json_encode($dat);
				}
			
			}else{
				
				$dat = ['code' => 401, 'msg' => '此任务状态错误,请重新传值'];
	        	return json_encode($dat);
			}
			
		}	
		
	}

	/*进行中任务列表*/
	public function ing(){
		$userID = input('id'); 
		$status = $this->status;
		if($status == "error"){
			 $dat = ['code' => 403, 'msg' => '登录超时,请重新登录'];
	        return json_encode($dat);
		}else{
			$statu = 1;
			$data = db('task')
				->alias('ta')
				->join('user us','ta.uid = us.id')
				->join('fbtask fb','ta.fbid = fb.id')
				->where('ta.uid',$userID)
				->where('status',$statu)
				->field('ta.id,ta.status,fb.money,us.rnumber,fb.type,fb.number')
				->select();
				if(count($data) !== 0){
					$dat = array('code'=>200,'msg'=>'返回数据成功',$data);
				    echo json_encode($dat,TRUE);		
				}else{
					$dat = array('code'=>402,'msg'=>'暂无数据');
				    echo json_encode($dat,TRUE);
				}	


		}
	}	



	/*提交任务*/
	public function refer()
	{	
		$userID = input('id'); 
		$taskid = input('fbid'); //任务id
		$img = input('img');	//结束图片
		$status = $this->status;
		if($status == "error"){
			 $dat = ['code' => 403, 'msg' => '登录超时,请重新登录'];
	        return json_encode($dat);
		}else{
			//验证提交
				//根据提交的任务编号查询当前用户的接单任务，判断任务是否提交，提交的话禁止提交
				if($userID !==  "" && $taskid !== "" && $img !== ""){
					$task = new Task;
					$isNULL = $task->where('uid',$userID)->where('id',$taskid)->where('status',4)->field(['img'])->select();
					$data = $isNULL->toArray();
					$res = array_pop($data);
					if($res['img'] == NULL){
						$status = "4";
						$update_time = date('Y-m-d: H:i:s',time());
						$isupdate = $task->where('uid',$userID)->where('id',$taskid)
							->update(['img'=>$img,'update_time'=>$update_time,'status'=>$status]);
							if($isupdate ){
								 $dat = ['code' => 200, 'msg' => '任务提交成功'];
            					 return json_encode($dat,TRUE);
								}
							
					}else{
						$dat = ['code' => 401, 'msg' => '任务已经提交,请无重复提交'];
            			return json_encode($dat,TRUE);
					}						
				}elseif($userID == "" || $taskid == ""){
					 		$dat = ['code' => 402, 'msg' => '当前用户id或者任务id未传参'];
            				return json_encode($dat,TRUE);
				}else{
							$dat = ['code' => 403, 'msg' => '请上传图片'];
            				return json_encode($dat,TRUE);
				}
		}


	}



	/*任务记录*/
	public function record(){
		//获取当前登录人员做过的任务
		$userID = input('id'); //用户的ID
		$statu = input('status'); //任务状态 1进行中2已完成
		$status = $this->status;
		if($status == "error"){
			 $dat = ['code' => 403, 'msg' => '登录超时,请重新登录'];
	        return json_encode($dat);
		}else{
					if($statu == "1" || $statu == "2" ){
							$data = db('task')
										->alias('ta')
										->join('user us','ta.uid = us.id')
										->join('fbtask fb','ta.fbid = fb.id')
										->where('ta.uid',$userID)
										->where('status',$statu)
										->field('ta.status,fb.money,us.rnumber,fb.type,fb.number,ta.status.ta.reason')
										->select();
								if(count($data) !== 0){
									$dat = array('code'=>200,'msg'=>'返回数据成功',$data);
				             		echo json_encode($dat,TRUE);		
								}else{
									$dat = array('code'=>402,'msg'=>'暂无数据');
				             		echo json_encode($dat,TRUE);
								}		
					}elseif($statu == "3"){
							$data = db('task')
									->alias('ta')
									->join('user us','ta.uid = us.id')
									->join('fbtask fb','ta.fbid = fb.id')
									->where('ta.uid',$userID)
									->field('ta.status,fb.money,us.rnumber,fb.type,fb.number,ta.status,ta.reason')
									->select();
								if(count($data) !== 0){
									$dat = array('code'=>200,'msg'=>'返回数据成功',$data);
				             		echo json_encode($dat,TRUE);		
								}else{
									$dat = array('code'=>402,'msg'=>'暂无数据');
				             		echo json_encode($dat,TRUE);
								}
					}

			}
	}


}